<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
  <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  <!-- <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/js/npm.js"></script> -->
</head>

<body>
  <div id="app">
    <div class="container">
      <h1 class="text-center text-info">信息输入</h1>
      <form>
        <div class="form-group">
          <label for="exampleInputEmail1">姓名</label>
          <input type="text" class="form-control" v-model="name">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">手机号</label>
          <input type="text" class="form-control" v-model="phone">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">备注信息</label>
          <input type="text" class="form-control" v-model="info">
        </div>
        <button type="button" class="btn btn-primary" @click="add">添加</button>
        <button type="button" class="btn btn-danger" @click="reset">重置</button>
      </form>
      <h1 class="text-center text-info">学员信息展示</h1>
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>姓名</th>
            <th>手机号</th>
            <th>备注</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in list" :key="index">
            <td>{{item.name}}</td>
            <td>{{item.phone}}</td>
            <td>{{item.info}}</td>
            <td>
              <button type="button" class="btn btn-danger" @click="del(index)">删除</button>
            </td>
          </tr>
          <tr>
            <td colspan="4">
              <button type="button" class="btn btn-danger" @click="delAll">删除全部</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        name: '',
        phone: '',
        info: '',
        list: JSON.parse(localStorage.getItem('infoList')) || []
      },
      methods: {
        // 添加学员
        add() {
          // 判空
          if(this.name&&this.phone&&this.info) {
            // 添加到数组中
            this.list.push({
              name: this.name,
              phone: this.phone,
              info: this.info
            });
            // 存入本地
            localStorage.setItem('infoList', JSON.stringify(this.list))
            // 重置输入框
            this.reset();
          }
        },
        // 重置表单
        reset() {
          this.name = '';
          this.phone = '';
          this.info = '';
        },
        // 删除
        del(index) {
          // 第一个参数是开始删除的位置，第二个是长度
          this.list.splice(index, 1);
          // 存入本地
          localStorage.setItem('infoList', JSON.stringify(this.list));
        },
        // 全部删除
        delAll() {
          this.list = [];
          // 存入本地
          localStorage.setItem('infoList', JSON.stringify(this.list));
        }
      },
    })
  </script>
</body>

</html>